package hoang.hut.networkdemo.network;

import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Utils {

	public static final boolean IS_DEBUG = true;

	public static void log(String message) {
		if (IS_DEBUG) {
			String fullClassName = Thread.currentThread().getStackTrace()[3]
					.getClassName();
			String className = fullClassName.substring(fullClassName
					.lastIndexOf(".") + 1);
			String methodName = Thread.currentThread().getStackTrace()[3]
					.getMethodName();
			int lineNumber = Thread.currentThread().getStackTrace()[3]
					.getLineNumber();

			System.out.println(className + "." + methodName + "():"
					+ lineNumber + " | " + message);
		}
	}

	public static void showAlertPositive(Context context, int icon,
			String title, String message) {
		AlertDialog.Builder dialog = new AlertDialog.Builder(context);
		dialog.setTitle(title);
		dialog.setMessage(message);
		if (icon != 0) {
			dialog.setIcon(icon);
		}
		dialog.setPositiveButton("OK", null);
		dialog.show();
	}

	/**
	 * Check network connection
	 * 
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager conMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo i = conMgr.getActiveNetworkInfo();
		if (i == null) {
			return false;
		}
		if (!i.isConnected()) {
			return false;
		}
		if (!i.isAvailable()) {
			return false;
		}
		return true;
	}
}
